/*
 * Package Restaurant has the variables and functions used in
 * oder files of that project
 */
package restaurant;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Thays Costa & Rafael Prallon
 */
public class Restaurant {

   
   
    static final String serverName = "localhost:3306/mydb";
    static final String user = "root";
    static final String password = "thays";
    
    
    
    private int row = 0;
    public ArrayList<Order> listOrders = new ArrayList<>();//todos os pedidos por codigo
    public ArrayList<String> kitchenOrders = new ArrayList<>();//todos os pedidos por ordem de chegada ->para a cosinha
    private java.sql.Connection con;
    private PreparedStatement stmt;
    
    public Restaurant(){
   
        try{

            con = new ConnectionFactory().getConnection(serverName,user,password);
       }
        catch(Exception e){
            System.out.println("Error: "+ e.getMessage());
        }
    }

    public void refresh(){
        readDish("Appetizer", "idOhA", "Order_has_Appetizer");
        readDish("Drink", "idOhDrink","Order_has_Drink");
        readDish("Salad", "idOhS","Order_has_Salad");      
        readDish("Dessert", "idOhDessert","Order_has_Dessert");      
        readDish("Meat", "idOhM","Order_has_Meat");        
        readDish("Pasta", "idOhP","Order_has_Pasta");        
        // montando a data através do Calendar
        //Calendar data = Calendar.getInstance();
        //data.setTime(rs.getDate("dataNascimento"));
        //contato.setDataNascimento(data);
    }
    public void restaurantClose(){
        try{
            con.close();
        }
        catch(SQLException e){
            System.out.println("Error: "+ e.getMessage());
        }
    }
    
    public void readDish(String dish, String idOhDish, String OhDish){
        try{
            stmt = con.prepareStatement("SELECT "+dish.toLowerCase()+"Name, "+dish.toLowerCase()+"Price, Order_idOrder, Order_Table_idTable FROM "+dish+","+OhDish+" WHERE ("+dish+"_id"+dish+" = id"+dish+")");
            ResultSet rs = stmt.executeQuery();

            // itera no ResultSet
            while (rs.next()) {
                // criando o objeto Contato
                int code = Integer.parseInt(rs.getString("Order_idOrder"));
                int index = thereIsThatCode(code);
                Order order = new Order();
                Item item = new Item();
                if(index==-1){
                    order.setTableNumber(Integer.parseInt(rs.getString("Order_Table_idTable")));
                    order.setCode(Integer.parseInt(rs.getString("Order_idOrder")));
                    item.setName(rs.getString(dish.toLowerCase()+"Name"));
                    item.setPrice(Double.parseDouble(rs.getString(dish.toLowerCase()+"Price")));
                    order.itens.add(item);
                    listOrders.add(order);
                }
                else{
                    item.setName(rs.getString(dish.toLowerCase()+"Name"));
                    item.setPrice(Double.parseDouble(rs.getString(dish.toLowerCase()+"Price")));
                    listOrders.get(index).itens.add(item);      
                }
                kitchenOrders.add(rs.getString("Order_Table_idTable")+rs.getString(dish.toLowerCase()+"Name"));
                System.out.println(rs.getString("Order_Table_idTable")+rs.getString(dish.toLowerCase()+"Name"));
            }
            stmt.close();
            rs.close();
        }
        catch (SQLException ex) {
            //Logger.getLogger(Restaurant.class.getName()).log(Level.SEVERE, null, ex);
             System.out.println("Error: "+ ex.getMessage());
 
        }      
    }
    
    public int thereIsThatCode(int cod){//procura se o cliente já está na lista e se sim retorna o indice
        Iterator<Order> i = listOrders.iterator();
        Order order = new Order();
        while(i.hasNext()){
             order = i.next();
            if(cod == order.getCode()){
                return listOrders.indexOf(order);
            }
        }
        return -1;
    }
    //public static void main(String[] args) {
        // TODO code application logic here
        //Restaurant r = new Restaurant();
        //r.refresh();
        //r.restaurantClose();
    //}
}
